
package cn.yr.controller.manage;

import cn.yr.common.constant.Constants;
import cn.yr.common.util.AjaxResult;
import cn.yr.domain.entity.SysMenu;
import cn.yr.domain.entity.SysUser;
import cn.yr.service.manage.ISysMenuService;
import cn.yr.service.manage.system.SysLoginService;
import cn.yr.service.manage.system.TokenService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 登录验证
 *
 * @author ruoyi
 */


@RestController
public class SysLoginController {
	@Resource
	private SysLoginService loginService;

	@Resource
	private ISysMenuService menuService;

	@Resource
	private TokenService tokenService;

	/**
	 * 登录方法
	 *
	 * @param username 用户名
	 * @param password 密码
	 * @param code
	 *
	 * 验证码
	 * @param uuid     唯一标识
	 * @return 结果
	 */

	@PostMapping("/login")
	public AjaxResult login(String username, String password, String code, String uuid) {
		AjaxResult ajax = AjaxResult.success();
		// 生成令牌
		String token = loginService.login(username, password, code);
		ajax.put(Constants.TOKEN, token);
		return ajax;
	}


	/**
	 * 获取用户信息
	 *
	 * @return 用户信息
	 */

	@GetMapping("getInfo")
	public AjaxResult getInfo() {
		SysUser user = tokenService.getUser();
		AjaxResult ajax = AjaxResult.success();
		ajax.put("user", user);
		return ajax;
	}

	/**
	 * 获取路由信息
	 *
	 * @return 路由信息
	 */
	@GetMapping("getRouters")
	public AjaxResult getRouters() {
		List<SysMenu> menus = menuService.selectMenuTree();
		return AjaxResult.success(menuService.buildMenus(menus));
	}

	@PostMapping("logout")
	public AjaxResult logout(){
		return AjaxResult.success();
	}
}

